package com.example.bailin.giftsay.hot.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.AppraiseEntity;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by bailin on 16/8/30.
 */
public class HotPLAdapter extends BaseAdapter {
    private AppraiseEntity entity;

    public void setEntity(AppraiseEntity entity) {
        this.entity = entity;
    }

    @Override
    public int getCount() {
        return entity.getData().getComments() == null ? 0 : entity.getData().getComments().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(MyApp.getContext());
        PLViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_hotpl, parent, false);
            viewHolder = new PLViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (PLViewHolder) convertView.getTag();
        }
        String date ="" + entity.getData().getComments().get(position).getCreated_at();
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");
        date = format.format(new Date(Long.valueOf(date)));
        NetTool tool = new NetTool();
        if (entity.getData().getComments().get(position).getUser().getAvatar_url().equals("")) {
            viewHolder.ivHotplAvatarUrl.setImageResource(R.mipmap.ig_profile_photo_default);
        } else {
        tool.getRoundImg(entity.getData().getComments().get(position).getUser().getAvatar_url(), viewHolder.ivHotplAvatarUrl);
        }
        viewHolder.tvHotplNickname.setText(entity.getData().getComments().get(position).getUser().getNickname());
        viewHolder.tvHotplCreatedAt.setText(date);
        viewHolder.tvHotplContent.setText(entity.getData().getComments().get(position).getContent());
        return convertView;
    }

    private class PLViewHolder{
        ImageView ivHotplAvatarUrl;
        TextView tvHotplNickname;
        TextView tvHotplCreatedAt;
        TextView tvHotplContent;

        public PLViewHolder(View view) {
            ivHotplAvatarUrl = (ImageView) view.findViewById(R.id.iv_hotpl_avatar_url);
            tvHotplNickname = (TextView) view.findViewById(R.id.tv_hotpl_nickname);
            tvHotplCreatedAt = (TextView) view.findViewById(R.id.tv_hotpl_created_at);
            tvHotplContent = (TextView) view.findViewById(R.id.tv_hotpl_content);
        }
    }
}
